package org.devocean.groupbuying.web.interceptors;

import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.LocaleEditor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.support.RequestContextUtils;

public class CustomerLocaleChangeInterceptor extends HandlerInterceptorAdapter {
	private String defaultLocale;

	/**
	 * Pre Intercept the request for uri language adaptation purpose
	 * <p>
	 * Actions :
	 * <p>
	 * - Replace language path variable ( Case : ContextPath/{lang}/***)
	 * <p>
	 * - Remove language path variable ( Case : ContextPath/{defaultLocale}/)
	 * <p>
	 * - Add Language path variable ( Case : ContextPath/**** | Home not
	 * included )
	 */
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler)
			throws ServletException {
		System.out.println("pre " + request.getRequestURI());

		String newLocale = request.getParameter("lang");
		if (newLocale == null) {
			if (request.getCookies() != null) {
				for (Cookie c : request.getCookies()) {
					if (c.getName().equalsIgnoreCase("clientLanguage")) {
						newLocale = c.getValue();
					}
				}

			}
		}

		LocaleResolver localeResolver = RequestContextUtils
				.getLocaleResolver(request);
		if (localeResolver == null) {
			throw new IllegalStateException(
					"No LocaleResolver found: not in a DispatcherServlet request?");
		}
		LocaleEditor localeEditor = new LocaleEditor();
		localeEditor.setAsText(newLocale != null ? newLocale
				: this.defaultLocale);
		localeResolver.setLocale(request, response,
				(Locale) localeEditor.getValue());

		return true;
	}

	/**
	 * Post Intercept the request for uri generation
	 * <p>
	 * Actions :
	 * <p>
	 * - Generate localPath with newLocale handle
	 */
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

		System.out.println("post " + request.getRequestURI());

		String newLocale = this.defaultLocale;
		if (request.getCookies() != null) {
			for (Cookie c : request.getCookies()) {
				if (c.getName().equalsIgnoreCase("clientLanguage")) {
					newLocale = c.getValue();
				}
			}

		}

		request.setAttribute("localPath", request.getContextPath() + "/"
				+ newLocale + "/");

		request.setAttribute("defaultLocalPath", request.getContextPath() + "/");

		super.postHandle(request, response, handler, modelAndView);
	}

	public String getDefaultLocale() {
		return defaultLocale;
	}

	public void setDefaultLocale(String defaultLocale) {
		this.defaultLocale = defaultLocale;
	}

}
